using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Net.NetworkInformation;
namespace Test {
	class Wmi {
		static void MainX() {

			Console.WriteLine(DateTime.Now.DayOfYear);

			List<string> data=new List<string>(100);
			List<string> servers=new List<string>(100);
			Wmi wmi=new Wmi();

			servers.Add("PON-WPMQ01-MQ");
			servers.Add("PON-WPAS10-SVC");
			servers.AddRange(Mixer.CombineEx("PON-WPAS{0:00}","1,5"));
			//data.AddRange(wmi.LogDiskSpace("PON-WPSQ21-DB05"));
			//servers.AddRange(Mixer.Combine("pon-wpct{0}","51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75"));
			servers.AddRange(Mixer.CombineEx("pon-wpwb{0:00}-cl0{1}","1-3","1,2"));
			servers.AddRange(Mixer.Combine("pon-wpsq{0}-db","08,13,16"));
			//servers.AddRange(Mixer.Combine("pon-wpsq21-cl{0}","02,03,04,05,06"));
			foreach(string srv in servers)
				data.AddRange(wmi.LogDiskSpace(srv));




			//local.NonQry("drop table DiskStat");
			//local.NonQry("create table DiskStat(id int identity,inserted datetime default getdate(),srv varchar(50),disk char,dtype int,label varchar(50),dsize bigint,dfree bigint,serial varchar(8))");
			//foreach(string line in data)
			//{
			//  string[] arr=line.Split(';');
			//  //Console.WriteLine(string.Join("; ",arr));
			//  local.NonQry("insert into DiskStat(disk,dtype,srv,label,dsize,dfree,serial)values('{0}',{1},'{2}','{3}',{4},{5},'{6}')",arr);
			//}
		}
		static void MainB() {
			List<string> serversToPing=new List<string>();
			//serversToPing.AddRange(Mixer.Combine("pon-wpmq01-{0}","cl01,cl02,mq"));
			//serversToPing.AddRange(Mixer.Combine("pon-wpsq{0}-cl0{1}","01,02,03,04,06,07,08,09,10,11,12,13,15,16","1,2"));
			//serversToPing.AddRange(Mixer.Combine("pon-wpsq21-cl{0}","02,03,04,05,06"));
			//serversToPing.AddRange(Mixer.Combine("pon-wpwb{0}-cl0{1}","01,02,03","1,2"));
			//serversToPing.AddRange(Mixer.Combine("pon-wpas10-{0}","cl01,cl02,svc"));
			serversToPing.AddRange(Mixer.Combine("pon-wpct{0}","51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75"));
			foreach(string str in serversToPing)
			{
				PingReply rep=new Ping().Send(str);
				Console.WriteLine("Ping "+str+":"+rep.Status+" "+rep.RoundtripTime+"ms");
			}
			Wmi wmi=new Wmi();
			List<string> serversDiskSpace=new List<string>();
			serversDiskSpace.AddRange(Mixer.Combine("pon-wpmq01-{0}","cl01,cl02,mq"));
			foreach(string srv in serversDiskSpace)
			{
				wmi.GetDiskspace(srv);
			}
		}
		public string[] LogDiskSpace(string machine) {
			List<string> data=new List<string>();
			ConnectionOptions options=new ConnectionOptions();
			ManagementScope scope=new ManagementScope(string.Format(@"\\{0}\root\cimv2",machine),options);
			scope.Connect();
			SelectQuery diskQuery=new SelectQuery("Select * from Win32_LogicalDisk");
			ManagementObjectSearcher diskSearcher=new ManagementObjectSearcher(scope,diskQuery);
			ManagementObjectCollection queryCollection=diskSearcher.Get();
			foreach(ManagementObject mo in queryCollection)
			{
				List<string> line=new List<string>();
				line.Add(mo["Name"].ToString().Replace(":",""));
				line.Add(mo["DriveType"].ToString());
				//line.Add(mo["DeviceID"].ToString());
				line.Add(mo["SystemName"].ToString());
				uint driveType=(uint)mo["DriveType"];

				if(driveType==5)
				{
					//line.Add("CD-ROM");
					//line.Add("-1");
					//line.Add("-1");
					//line.Add("FFFFFFFF");
					//Console.WriteLine("CD-ROM");
				}
				else if(driveType==2)
				{
					//line.Add("Floppy");
					//line.Add("-1");
					//line.Add("-1");
					//line.Add("FFFFFFFF");
				}
				else if(driveType==3)
				{
					line.Add(mo["VolumeName"].ToString());
					line.Add(mo["Size"].ToString());
					line.Add(mo["FreeSpace"].ToString());
					line.Add(mo["VolumeSerialNumber"].ToString());
					new DAL().NonQry("insert into DiskStat(disk,dtype,srv,label,dsize,dfree,serial)values('{0}',{1},'{2}','{3}',{4},{5},'{6}')",line.ToArray());
					data.Add(string.Join(";",line.ToArray()));

				}
				else
					throw new Exception("Add a new drive type!");


			}

			return data.ToArray();
		}
		public void GetDiskspace(string machine) {
			ConnectionOptions options=new ConnectionOptions();
			ManagementScope scope=new ManagementScope(string.Format(@"\\{0}\root\cimv2",machine),options);
			scope.Connect();
			//ObjectQuery query=new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
			SelectQuery query1=new SelectQuery("Select * from Win32_LogicalDisk");
			//ManagementObjectSearcher searcher=new ManagementObjectSearcher(scope,query);
			//ManagementObjectCollection queryCollection=searcher.Get();
			ManagementObjectSearcher searcher1=new ManagementObjectSearcher(scope,query1);
			ManagementObjectCollection queryCollection1=searcher1.Get();
			//foreach(ManagementObject m in queryCollection)
			//{
			//  // Display the remote computer information
			//  Console.WriteLine("Computer Name : {0}",m["csname"]);
			//  Console.WriteLine("Windows Directory : {0}",m["WindowsDirectory"]);
			//  Console.WriteLine("Operating System: {0}",m["Caption"]);
			//  Console.WriteLine("Version: {0}",m["Version"]);
			//  Console.WriteLine("Manufacturer : {0}",m["Manufacturer"]);
			//  Console.WriteLine();
			//}
			foreach(ManagementObject mo in queryCollection1)
			{
				// Display Logical Disks information
				Console.WriteLine("              Disk Name : {0}",mo["Name"]);
				Console.WriteLine("              Disk Type : {0}",mo["DriveType"]);
				if((uint)mo["DriveType"]!=5)
				{
					Console.WriteLine("        Disk VolumeName : {0}",mo["VolumeName"]);
					Console.WriteLine("              Disk Size : {0:0.0000}Gb",float.Parse(mo["Size"].ToString())/1024/1024/1024);
					Console.WriteLine("              FreeSpace : {0:0.0000}Gb",float.Parse(mo["FreeSpace"].ToString())/1024/1024/1024);
					Console.WriteLine("          Disk DeviceID : {0}",mo["DeviceID"]);
					Console.WriteLine("        Disk SystemName : {0}",mo["SystemName"]);
					Console.WriteLine("Disk VolumeSerialNumber : {0}",mo["VolumeSerialNumber"]);
					Console.WriteLine();
				}
				else
					Console.WriteLine("CD-ROM");
			}
		}
	}
}
